<script setup lang='ts'>
  import { nextTick, reactive, ref } from 'vue';
  import { guild_roomApi } from '@/api/controller';
  import { ToastSuccess, Toastloading } from '@/utils/public';
  import serachRoomid from '@/components/serachRoomid/index.vue';
  const formLabelWidth = '120px'
  const dataForm = ref()
  const $emit = defineEmits(['updateList'])
  const action = reactive({
    dialogFormVisible:false,
    disabled:false,
    title:'',
  })
  interface formQuert {
    id:number
    room_uid:number
    room_id:number
    close_reason:string
  }
  let form = reactive({} as formQuert)
  const formRules = {
    room_uid: [{ required: true, message: "请输入房间UID", trigger: "blur" }],
    close_reason: [{ required: true, message: "请输入关闭房间理由", trigger: "blur" }],
  }
  const handleSubmit = ()=>{
    dataForm.value.validate(async(valid:any)=>{
      if(!valid) return
      const loading = Toastloading()
      let submitSet = false
      const { id } = form
      let newForm = {...form}
      if(id) {
        await guild_roomApi.editCloseRoomReason(newForm).then(()=>{
          ToastSuccess('编辑成功')
          submitSet = true
        }).catch(()=>{
          loading.close()
        })
      }else {
        await guild_roomApi.closeRoomReason(newForm).then(()=>{
          ToastSuccess('关闭成功')
          submitSet = true
        }).catch(()=>{
          loading.close()
        })
      }
      if(submitSet) {
        loading.close()
        closeDialog()
        $emit('updateList')
      }
    })
  }
  const openDialog = async(query:any = {})=> {
    let {
      id=0, desc:close_reason='',room_uid=0
    } = query
    let formQuery = {

    }
    if(id) formQuery = {
      id,
      room_uid,
      close_reason
    }
    Object.assign(form,formQuery)
    action.title = id?'编辑房间关闭理由':'关闭房间'
    action.dialogFormVisible = true
  }
  const closeDialog = ()=>{
    form = reactive({} as formQuert)
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
      top="10vh"
      class="xm-dialog input-width"
      v-model="action.dialogFormVisible"
      :close-on-click-modal="action.disabled"
      :show-close="action.disabled"
      :close-on-press-escape="action.disabled"
      width="52%"
      :title="action.title">
      <el-form ref="dataForm" :model="form" :rules="formRules">
        <el-form-item v-if="!form.id" label="房间UID" :label-width="formLabelWidth" prop="room_uid">
          <serachRoomid v-model:room_uid="form.room_uid" />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="关闭理由" prop="close_reason">
          <el-input
            v-model="form.close_reason"
            :autosize="{ minRows: 3, maxRows: 3}"
            type="textarea"
            placeholder="请输入关闭房间理由"
          />
        </el-form-item>
      </el-form>
      <template v-if="!action.disabled" #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>